package com.nuc.workflowserver.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nuc.workflowserver.common.model.ResultModel;
import com.nuc.workflowserver.common.util.CommonUtils;
import com.nuc.workflowserver.mapper.IUserInfoMapper;
import com.nuc.workflowserver.mapper.IUserMapper;
import com.nuc.workflowserver.mapper.IUserRoleMapper;
import com.nuc.workflowserver.model.dto.UserInfoDTO;
import com.nuc.workflowserver.model.pojo.User;
import com.nuc.workflowserver.model.pojo.UserInfo;
import com.nuc.workflowserver.model.pojo.UserRole;
import com.nuc.workflowserver.model.vo.UserInfoVO;
import com.nuc.workflowserver.service.IUserInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class IUserInfoServiceImpl implements IUserInfoService {

    @Autowired
    IUserInfoMapper iUserInfoMapperl;

    @Autowired
    IUserMapper iUserMapper;

    @Autowired
    IUserRoleMapper iUserRoleMapper;

    @Override
    public PageInfo<UserInfo> selectPage(UserInfo userInfo, Integer pageNum,
            Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<UserInfo> list = iUserInfoMapperl.selectList(userInfo);
        PageInfo<UserInfo> page = new PageInfo<>(list);
        return page;
    }

    @Override
    public UserInfo selectByPrimary(UserInfo userInfo) {
        return iUserInfoMapperl.selectByPrimary(userInfo);
    }

    @Override
    public Integer update(UserInfo userInfo) {
        return iUserInfoMapperl.update(userInfo);
    }

    @Override
    public Integer delete(UserInfo userInfo) {
        return iUserInfoMapperl.delete(userInfo);
    }

    @Override
    public List<UserInfo> selectList(UserInfo userInfo) {
        return null;
    }

    @Override
    public Integer insert(UserInfo userInfo) {
        return null;
    }

    @Override
    @Transactional
    public ResultModel insertOrUpadte(UserInfoDTO userInfoDTO) {
        if (null == userInfoDTO.getId() || ""
                .equals(userInfoDTO.getId())) {  //新增
            UserInfo checkUserInfo = new UserInfo();
            checkUserInfo.setId(userInfoDTO.getUserId());
            UserInfo res = iUserInfoMapperl.selectByPrimary(checkUserInfo);
            if (null != res) {
                return new ResultModel("该员工编号已存在！", false);
            }
            userInfoDTO.setId(CommonUtils.getUUID());
            UserInfo userInfo = new UserInfo();
            BeanUtils.copyProperties(userInfoDTO, userInfo);
            userInfo.setValid(true);
            Integer userInfoRes = iUserInfoMapperl.insert(userInfo);
            User user = new User();
            user.setId(userInfoDTO.getUserId());
            user.setUserName(userInfoDTO.getUsername());
            user.setPassword(userInfoDTO.getPassword());
            Integer userRes = iUserMapper.insert(user);
            UserRole userRole = new UserRole();
            userRole.setId(CommonUtils.getUUID());
            userRole.setUserId(userInfoDTO.getUserId());
            userRole.setRoleId(userInfoDTO.getRoleId());
            Integer userRoleRes = iUserRoleMapper.insert(userRole);
            if (userInfoRes > 0 && userRes > 0 && userRoleRes > 0) {
                return new ResultModel("新增成功！", true);
            } else {
                return new ResultModel("新增失败，请联系管理员！", false);
            }
        } else {  //更新
            UserInfo upUserInfo = new UserInfo();
            upUserInfo.setId(userInfoDTO.getId());
            upUserInfo.setPhone(userInfoDTO.getPhone());
            upUserInfo.setAddr(userInfoDTO.getAddr());
            Integer upUserInfoRes = iUserInfoMapperl.update(upUserInfo);
            User upUser = new User();
            upUser.setId(userInfoDTO.getUserId());
            upUser.setPassword(userInfoDTO.getPassword());
            Integer upUserRes = iUserMapper.update(upUser);
            if (upUserInfoRes > 0 && upUserRes > 0) {
                return new ResultModel("编辑成功！", true);
            } else {
                return new ResultModel("编辑失败，请联系管理员！", false);
            }
        }
    }

    @Override
    public UserInfoVO selectUserInfoVOByPrimary(String id) {
        return iUserInfoMapperl.selectUserInfoVOByPrimary(id);
    }
}
